<template>
  <div class="list">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 第一行内容 -->
      <el-row :gutter="20">
        <!-- 搜索框 -->
        <el-col :span="9">
          <el-input
            placeholder="请输入内容"
            v-model="searchInputValue"
            clearable
            @clear="clearSearchInputValue"
          >
            <el-button slot="append" icon="el-icon-search" @click="searchGoodsList"></el-button>
          </el-input>
        </el-col>

        <!-- 添加商品按钮 -->
        <el-col :span="3">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格视图 -->
      <el-table :data="goodsListData" style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="120"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'List',
  data() {
    return {
      // 搜索框内容
      searchInputValue: '',
      // 保存商品列表数据容器
      goodsListData: [],
      // 请求商品列表数据的参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 获取商品列表参数的总条数
      total: 0
    }
  },
  created() {
    this.getGoodsListData()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsListData() {
      // 根据参数获取商品列表数据
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 为商品数据列表总数容器赋值
      this.total = res.data.total
      // 给商品列表数据容器赋值
      this.goodsListData = res.data.goods
    },
    // 修改每页大小时触发
    handleSizeChange(newPageSize) {
      // 重置每页大小
      this.queryInfo.pagesize = newPageSize
      // 刷新表格
      this.getGoodsListData()
    },
    // 修改页码时触发
    handleCurrentChange(newPageNum) {
      // 更新页码
      this.queryInfo.pagenum = newPageNum
      // 刷新页面
      this.getGoodsListData()
    },
    // 点击搜索按钮搜索商品列表
    searchGoodsList() {
      // 如果搜索框为空格或者为空，则不进行数据请求
      if (this.searchInputValue.trim().length === 0) {
        return this.$message.info('请输入要搜索的商品关键字！')
      }
      // 将搜索框的内容赋值给请求数据参数的query
      this.queryInfo.query = this.searchInputValue
      // 更新商品列表数据
      this.getGoodsListData()
    },
    // 清除搜索框时触发
    clearSearchInputValue() {
      // 将数据请求参数query重置为0
      this.queryInfo.query = ''
      // 然后进行数据请求
      this.getGoodsListData()
    },
    // 删除商品
    async deleteGoods(id) {
      // 获取弹框操作结果
      const deleteGoodsResult = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (deleteGoodsResult === 'cancel')
        return this.$message.info('已取消删除！')
      // 实现商品删除
      const { data: res } = await this.$http.delete(`goods/${id}`)
      // 判断删除是否成功
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 提示删除成功
      this.$message.success('删除商品成功！')
      // 更新商品列表
      this.getGoodsListData()
    },
    // 跳转到添加商品页面组件
    addGoods(){
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style scoped>
</style>